iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 10
0

04-Operators

playground9.dart

Code

void main() {
  int num = 10 + 22;
  num = num - 2;

  print(num);

  num = num % 5;
  print(num);

  // relational ==, !=, >=, <=
  if (num == 0) {
    print('Zero');
  }

  num = 100;
  num *= 2; // num = num * 2
  print(num);

  // unary operator
  ++num;
  num++;
  num += 1;
  num -= 1;
  print(num);

  // logical && and logical ||
  if (num > 200 && num < 203) {
    print('200 to 202');
  }

  // != Not Equal
  if (num != 100) {
    print('num is not equal to 100');
  }
}

Output

30
0
Zero
200
202
200 to 202
num is not equal to 100

playground10.dart

  • Null Aware Operator
  • (?.), (??), (??=)

Code

class Num {
  int num = 10;
}

main() {
  var n = Num();
  int number;

  if (n != null) {
    number = n.num;
  }

  print(number);

// part2 -- null error
  // var n2;
  // int number2;

  // number = n2.num;

  // print(number2);

// part3
  var n3 = Num();
  int number3;

  number3 = n3?.num;

  print(number3);

// part4
  var n4;
  int number4;

  number4 = n4?.num ?? 0;

  print(number4);

// part5
  int number5;
  print(number5 ??= 100);
  print(number5);
}

Output

10
10
0
100   /* ??= */
100

playground11.dart

  • Ternary operator

Code

void main() {
  int x = 100;
  var result = x % 2 == 0 ? 'Even' : 'Odd';
  print(result);
}

Output

Even

playground12.dart

  • Type test

Code

void main() {
  int x = 100;

  if (x is int) {
    print('integer');
  }
}

Output

integer

playground13.dart

  • Conditional Statement

Code

void main() {
  int number = 100;

  if (number % 2 == 0) {
    print('Even');
  } else if (number % 3 == 0) {
    print('Odd');
  } else {
    print('Confused');
  }
}

Output

Even

playground14.dart

  • Conditional Statement

Code

void main() {
  int number = 20;
  switch (number) {
    case 0:
      print('Even');
      break;

    case 1:
      print('Odd');
      break;

    default:
      print('Confused');
  }
}

Output

Confused

05-Loop

playground15.dart

  • Loop

Code

void main() {
  // Standard for loop
  for (var i = 1; i <= 10; ++i) {
    print(i);
  }
}

Output

1
2
3
4
5
6
7
8
9
10

playground16.dart

Code

void main() {
  // for-in loop
  var numbers = [1, 2, 3];

  for (var n in numbers) {
    print(n);
  }

  for (var i = 0; i < numbers.length; ++i) {
    print(numbers[i]);
  }
}

Output

1
2
3
1
2
3

playground17.dart

Code

void main() {
  // forEach loop
  var numbers = [1, 2, 3];

  numbers.forEach((n) => print(num));
}

Output

num
num
num

playground18.dart

Code

void main() {
  // forEach loop
  var numbers = [1, 2, 3];

  numbers.forEach((n) => print(n));
}

Output

1
2
3

playground19.dart

Code

void main() {
  // forEach loop
  var numbers = [1, 2, 3];

  numbers.forEach(printNum);
}

void printNum(num) {
  print(num);
}

Output

1
2
3

playground20.dart

Code

void main() {
  // while loop
  int num = 5;

  while (num > 0) {
    print(num);
    num -= 1;
  }
}

Output

5
4
3
2
1

playground21.dart

Code

void main() {
  for (var i = 0; i < 10; ++i) {
    if (i > 5) break;
    print(i);
  }
}

Output

0
1
2
3
4
5

playground22.dart

Code

void main() {
  for (var i = 0; i < 10; ++i) {
    if (i % 2 == 0) continue;
    print('Odd: $i');
  }
}

Output

Odd: 1
Odd: 3
Odd: 5
Odd: 7
Odd: 9

06-Collection[List, Set, Map]

playground23.dart

Code

void main() {
  // List
  List names = ['Jack', 'Jill', 10, 100.1];

  // Only Type: String
  // List<String> names = ['Jack', 'Jill'];
  print(names[0]);
  print(names.length);

  for (var n in names) {
    print(n);
  }

  names[1] = 'Mark';

  for (var n in names) {
    print(n);
  }

  var names2 = [...names];
  names[0] = 'Marry';

  for (var n in names2) {
    print(n);
  }
}

Output

Jack
4
Jack
Jill
10
100.1
Jack
Mark
10
100.1
Jack
Mark
10
100.1

playground24.dart

Code

void main() {
  // Set
  var halogens = {'fluorine', 'chlorine'};

  for (var x in halogens) {
    print(x);
  }
}

Output

fluorine
chlorine

playground25.dart

Code

void main() {
  // Map
  var gifts = {
    // Key:     Value
    'first': 'partridge',
    'secend': 'turtledoves',
    'fifth': 'golden rings'
  };
  print(gifts['fifth']);
}

Output

golden rings

playground26.dart

Code

void main() {
  // Map
  var gifts = {
    // Key:     Value
    2: 'partridge',
    1: 'turtledoves',
    3: 'golden rings'
  };
  print(gifts[1]);
}

Output

turtledoves

playground27.dart

Code

void main() {
  // Map
  var gifts = Map();
  gifts['first'] = 'Mongo';

  print(gifts['first']);
}

Output

Mongo

playground28.dart

Code

void main() {
  // Map
  var gifts = {'first': 'Mongo', 'second': 'Jack Fruit'};

  print(gifts['second']);
}

Output

Jack Fruit

上一篇
Dart 語言 | 基本練習(上)
下一篇
Dart語言 | 基本練習 (下)
系列文
Dart & Flutter - 學寫App | 菜鳥筆記 | 堅持30天挑戰12
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言